// 所有可拖拽表单组件实例得基类

import { assignInWith } from 'lodash'
import type { formElementType } from '../form-render/form-item/form-item-render.ts'
import type { baseConfigType } from '../../../types/index.ts'
import { entityType } from './index.ts'
import type { TableColumnData, TableData } from '@arco-design/web-vue'

export interface configType {
    field: string
    label: string
    hidden?: boolean
    hideLabel:boolean
    required?: boolean
    [key: string]: any
}

export interface slotFormConfigType {
    modelValue:any

    [key: string]: any
}
export default class BaseEntity {
    public type:entityType

    //给予表单formItem中表单元素得配置例如
    public config: configType = {} as configType

    //给予表单formItem中表单元素得配置例如 
    public slotFormConfig: slotFormConfigType = {} as slotFormConfigType

    //用于生成右侧表单组件可配置项 所以该配置项一定是根据表单组件所拥有得属性来定义的 如果是第三方得组件则可以参考其官网对应组件所拥有得属性来配置 ，如果是自定义组件则根据组件所定义得props属性来定义
    public componentConfig: baseConfigType = {
        field: {
          type:'input',
          formItemConfig:{
            label:'字段Key',
            disabled:true,
          },
          formItemElement:{
             modelValue:'',
             placeholder:'请输入',
          }
        },
        label: {
          type:'input',
          formItemConfig:{
            label:'标签名称',
          },
          formItemElement:{
             modelValue:'',
             placeholder:'请输入',
          }
        },
        hideLabel: {
          type:'switch',
          formItemConfig:{
            label:"隐藏标签",
          },
          formItemElement:{
             modelValue:false,
          }
        },
        required: {
          type:'switch',
          formItemConfig:{
            label:'必填',
          },
          formItemElement:{
             modelValue:false,
          }
        }
    }
    constructor(type:entityType, config, slotFormConfig,componentConfig) {
        this.type = type
        this.config = config
        this.slotFormConfig = slotFormConfig
        this.initComponentConfig(type,componentConfig)
 
    }

      //将拖拽表单组件得初始化值赋值给右侧表单配置中组件中
    initComponentConfig(type:entityType,componentConfig){

      //如果是布局组件不适用默认得该配置项 需要自行在实例中定义
      if(type=='space-layout'){
        this.componentConfig =componentConfig
      }else{
        this.componentConfig = (assignInWith(this.componentConfig,componentConfig))
        this.componentConfig.label&& ( this.componentConfig.label.formItemElement.modelValue = this.config.label)
      }
      this.componentConfig.field.formItemElement.modelValue = this.config.field
      this.componentConfig.hideLabel&& (this.componentConfig.hideLabel.formItemElement.modelValue= this.config["hideLabel"])
      this.componentConfig.required&& (this.componentConfig.required.formItemElement.modelValue= (this.config["required"])||false)
    }

   
}
